home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 8418 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.2 KB  |  82 lines

  1. Path: gail.ripco.com!mambuhl
  2. From: mambuhl@ripco.com (Martin Ambuhl)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Passing Structures As
  5. Date: 3 Mar 1996 21:07:10 GMT
  6. Organization: Ripco Communications, Inc.
  7. Message-ID: <4hd1lu$1tp@gail.ripco.com>
  8. NNTP-Posting-Host: golden.ripco.com
  9.  
  10. razine@aol.com (Razine) in <4hcov3$h1d@newsbf02.news.aol.com> asks:
  11.  
  12. >I recently ran into a problem passing a structure as a pointer to a
  13. >function.  I then wanted to pass a indivdual variable to another function
  14. >but it didnt work.  Here is a brief example.  Can anyone spot anything
  15. >wrong.
  16.  
  17. As usual, my comments and changes to your code are marked with the
  18. comment tag `/* mha - ... */'.  There are minor formatting changes also:
  19.  
  20. #include <stdio.h>              /* mha - added.  Needed for printf(). */
  21.  
  22. typedef struct {
  23.     char name[81];
  24.     int address_num;
  25. }   person_rec;
  26.  
  27. void display_person(person_rec * pr);   /* mha - added prototype.
  28.                                          * Otherwise, you will have
  29.                                          * type mis-matches between the
  30.                                          * implicit and explicit
  31.                                          * declarations of
  32.                                          * display_person  */
  33.  
  34. int /* mha - was `void' */ main(void)
  35. {
  36.     person_rec thisuser = {"The Beast", 666};   /* mha - added
  37.                                                  * initialization so the
  38.                                                  * program actually
  39.                                                  * tests something */
  40.     display_person(&thisuser);
  41.     return 0;                   /* mha - added explicit return
  42.                                  * [optional] */
  43. }
  44.  
  45. void display_address(int address)
  46. {
  47.     printf("Address Number is %d\r\n", address);    /* mha - fixed `Adress'
  48.                                                      * [a nit] */
  49.     /* mha - removed unnecessary `return;' [a nit] */
  50. }
  51.  
  52. void display_person(person_rec * pr)
  53. {
  54.  
  55.     printf("Persons Name : %s\r\n", pr->name);  /* another question why
  56.                                                  * in this instance
  57.                                                  * would I have to use
  58.                                                  * the -> operand? */
  59.     /* mha - because pr is a pointer.  I don't know what you want to
  60.      * use instead. (*pr).name is possible, but that's just a synonym.
  61.      * BTW, when you try to printf() an unintialized auto char[] as you
  62.      * are doing here, you are lucky not to get a run-time error.  I
  63.      * have added an initialization above. */
  64.  
  65.     display_address(pr->address_num);
  66.     /* Basically I get an error on the line above, how would I just
  67.      * pass the interger value contained in pr->address_num */
  68.  
  69.     /* mha - What error do you get? pr->address_num is an int, and
  70.      * display_address expects an int. If you get an diagnostic from
  71.      * the compiler, I would suspect the compiler of having a problem.
  72.      * If it is a run-time error, then the absence of a prototype,
  73.      * added above, is the likely cause. */
  74.  
  75.     /* mha - removed unnecessary `return;' [a nit] */
  76. }
  77.  
  78.                                                                               
  79. --
  80. * Martin Ambuhl       net: mambuhl@ripco.com
  81. * Chicago, IL (USA)    
  82.